<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '商品添加' : '商品编辑'"
      :visible.sync="obj.isTrue"
      width="50%"
      @close="cancel()"
      @opened="opened()"
    >
      <!-- opened 对话框动画加载完毕后 -->
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="一级分类">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择一级分类"
            @change="changeCate()"
          >
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :value="item.id"
              :label="item.catename"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select v-model="form.second_cateid" placeholder="请选择二级分类">
            <el-option
              v-for="(item, index) in secondCate"
              :key="index"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="form.goodsname"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="市场价格">
          <el-input v-model="form.market_price"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <div class="fileBox1">
            <h2>+</h2>
            <input type="file" @change="changeImg1" v-if="obj.isTrue" />
            <img :src="imgUrl" alt="" v-if="imgUrl" />
          </div>
        </el-form-item>
        <el-form-item label="商品规格">
          <el-select
            v-model="form.specsid"
            placeholder="请选择商品规格"
            @change="changeSpecs()"
          >
            <el-option
              v-for="item in specsList"
              :key="item.id"
              :label="item.specsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格属性">
          <el-select
            multiple
            v-model="form.specsattr"
            placeholder="请选择规格属性"
          >
            <el-option
              v-for="(item, index) in attrsArr"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否新品">
          <el-radio :label="1" v-model="form.isnew">是</el-radio>
          <el-radio :label="2" v-model="form.isnew">否</el-radio>
        </el-form-item>
        <el-form-item label="是否热卖">
          <el-radio :label="1" v-model="form.ishot">是</el-radio>
          <el-radio :label="2" v-model="form.ishot">否</el-radio>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
        <el-form-item label="商品描述">
          <!-- 置入富文本编辑器 -->
          <div id="div1" v-if="obj.isTrue"></div>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button v-if="obj.isAdd" @click="add()" type="primary"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="edit()">编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import E from "wangeditor"; //导入富文本编辑器
import { mapGetters, mapActions } from "vuex";
import { cateList, goodsAdd, goodsEdit, goodsInfo } from "../../../request/api";
import { successAlert, warningAlert } from "../../../utils/alert";
export default {
  props: ["obj"],
  data() {
    return {
      form: {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: [],
        isnew: 1,
        ishot: 1,
        status: 1,
      },
      //回显图片用
      imgUrl: "",
      //用于渲染二级分类
      secondCate: [],
      //将所有属性规格保存到数组中 渲染到页面用
      attrsArr: [],
    };
  },
  methods: {
    ...mapActions({
      asyncSpecsList: "specs/asyncChangeList",
      asyncCateList: "cate/asyncChangeList",

      asyncGoodsList: "goods/asyncChangeList",
      asyncGoodsCount: "goods/asyncChangeCount",
    }),
    cancel() {
      (this.form = {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: [],
        isnew: 1,
        ishot: 1,
        status: 1,
      }),
        (this.obj.isTrue = false),
        (this.attrsArr = []),
        (this.imgUrl = "");
    },
    add() {
      let obj = {
        ...this.form,
        specsattr: JSON.stringify(this.form.specsattr),
        description: this.editor.txt.html(),
      };
      goodsAdd(obj).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          //重新请求列表数据
          this.asyncGoodsList();
          //   重新请求总条数
          this.asyncGoodsCount();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    getInfo(id) {
      goodsInfo({ id: id }).then((res) => {
        this.form = {
          ...res.data.list,
          specsattr:JSON.parse(res.data.list.specsattr),
          id: id,
        };
        //图片回显
        this.imgUrl = this.$pre + this.form.img
        //根据一级菜单的pid渲染二级菜单
        cateList({ pid: this.form.first_cateid }).then((res) => {
          this.secondCate = res.data.list
        });
        //渲染商品描述
        //渲染规格属性 和商品规格变化时做的一样
        let obj =this.specsList.find(item=>{
          return item.id == this.form.specsid
        })
        this.attrsArr = obj.attrs
      });
    },
    edit() {
      goodsEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.asyncGoodsList();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    //在对话框动画结束后 加载富文本编辑器
    opened() {
      // 加载编辑器
      this.editor = new E("#div1");
      this.editor.create();
      //将form.description内容放入编辑器内
      this.editor.txt.html(this.form.description);
    },
    //一级分类变化时
    changeCate() {
      cateList({ pid: this.form.first_cateid }).then((res) => {
        //先清空上次的二级菜单的id
        this.form.second_cateid = "";
        //再向二级菜单里赋新的内容
        this.secondCate = res.data.list;
      });
    },
    //图片上传框变化时
    changeImg1(e) {
      let file = e.target.files[0];
      //将图片回显出来
      this.imgUrl = URL.createObjectURL(file);
      //给form中的img赋值
      this.form.img = file;
    },
    //商品规格变化时
    changeSpecs() {
      //先清空上次选择的规格属性内容
      this.form.specsattr = "";
      let obj = this.specsList.find((item) => {
        return item.id == this.form.specsid;
      });
      this.attrsArr = obj.attrs;
    },
  },
  computed: {
    ...mapGetters({
      cateList: "cate/list",
      specsList: "specs/list",

      goodsList: "goods/list",
      goodsCount: "goods/count",
      goodsSize: "goods/size",
    }),
  },
  mounted() {
    this.asyncCateList(), this.asyncSpecsList(true);
  },
};
</script>

<style scoped>
.smBox {
  display: flex;
}
.smBox input {
  flex: 1;
}
.fileBox1 {
  width: 200px;
  height: 200px;
  border: 1px dashed #ccc;
  position: relative;
}
.fileBox1 h2 {
  text-align: center;
  line-height: 200px;
  font-size: 60px;
  margin: 0;
}
.fileBox1 input {
  width: 200px;
  height: 200px;
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
  z-index: 3;
}
.fileBox1 img {
  width: 200px;
  height: 200px;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 2;
}
</style>